namespace SimpleWebServices.Web.UI.Html
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("input")]
    public abstract class Input : Control, IAttributeAccesskey, IAttributeAlternativeText, IAttributeReadOnly, IXmlSerializable
    {
        protected Input()
        {
        }

        protected Input(Name id)
            : base(id)
        {
        }

        public char? Accesskey
        {
            get;
            set;
        }

        public string AlternativeText
        {
            get;
            set;
        }

        public bool ReadOnly
        {
            get;
            set;
        }

        public int? Size
        {
            get;
            set;
        }

        [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "The naming is intentionally the same as HTML.")]
        public abstract string Type { get; }

        public override string Value
        {
            get;
            set;
        }

        public override void ReadXml(XmlReader reader)
        {
            base.ReadXml(reader);

            if (!this.Type.Equals(reader.GetAttribute("type"), StringComparison.OrdinalIgnoreCase))
            {
                throw new XmlException("The input is of an unexpected type.");
            }

            if (XmlNodeType.Element == reader.NodeType)
            {
                string value = reader.GetAttribute("accesskey");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Accesskey = value[0];
                }

                value = reader.GetAttribute("alt");
                if (!string.IsNullOrEmpty(value))
                {
                    this.AlternativeText = value;
                }

                value = reader.GetAttribute("size");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Size = XmlConvert.ToInt32(value);
                }

                this.ReadOnly = "readonly".Equals(reader.GetAttribute("readonly"), StringComparison.OrdinalIgnoreCase);

                this.Value = reader.GetAttribute("value");
            }
        }

        public override void WriteXml(XmlWriter writer)
        {
            base.WriteXml(writer);

            if (this.Accesskey.HasValue)
            {
                writer.WriteAttributeString("accesskey", this.Accesskey.Value.ToString());
            }

            if (!string.IsNullOrEmpty(this.AlternativeText))
            {
                writer.WriteAttributeString("alt", this.AlternativeText);
            }

            if (this.ReadOnly)
            {
                writer.WriteAttributeString("readonly", "readonly");
            }

            if (this.Size.HasValue)
            {
                writer.WriteAttributeString("size", XmlConvert.ToString(this.Size.Value));
            }

            writer.WriteAttributeString("type", this.Type);
            writer.WriteAttributeString("value", this.Value);
        }
    }
}